package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.CheckItem;
import com.itheima.VO.Checkgroup_ids;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.checkgroupMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.checkgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass =checkgroupService.class )
@Transactional
public class checkgroupServiceImpl implements checkgroupService {
    @Autowired
    private checkgroupMapper checkgroupMapper;
    @Override
    public Result findAllGroup() {
        List<CheckItem> allCheckitem = checkgroupMapper.findAllCheckitem();
        if (allCheckitem !=null&&allCheckitem.size()>0){
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,allCheckitem);
        }else {
            return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @Override
    public void addCheckgroup(Checkgroup_ids checkgroup_ids) {
//{"formData":{"code":"1234123","name":"423134","helpCode":"1234","remark":"1234","attention":"1234","sex":"1"},"checkitemIds":[28,29,30]}
      checkgroupMapper.addCheckgroup(checkgroup_ids.getFormData());
        Integer checkgroupId = checkgroup_ids.getFormData().getId();
        System.out.println(checkgroupId);
        Integer[] checkitemIds = checkgroup_ids.getCheckitemIds();
        if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
             //   checkgroupMapper.setCheckGroupAndCheckItem(checkgroupId,checkitemId);
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkgroupId);
                map.put("checkitem_id",checkitemId);
                checkgroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
      Page<CheckGroup> checkGroupPage= checkgroupMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(checkGroupPage.getTotal(),checkGroupPage.getResult());
    }

    @Override
    public CheckGroup findCheckgroupById(Integer id) {
      CheckGroup checkGroup=  checkgroupMapper.findCheckgroupById(id);
        return checkGroup;
    }

    @Override
    public Integer[] findCehckitemsByCheckgroupId(Integer id) {
        Integer[] ids=   checkgroupMapper.findCehckitemsByCheckgroupId(id);
        return ids;
    }

    @Override
    public void edit(CheckGroup formData, Integer[] checkitemIds) {
        //因为原来的检查组也有自己的检查项,我们需要删除掉原来的检查项,在添加新的检查项
        checkgroupMapper.deleteAssociation(formData.getId());
        //添加新的关联关系
        if (checkitemIds!=null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",formData.getId());
                map.put("checkitem_id",checkitemId);
                checkgroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
        //最后修改检查项的基本信息
        checkgroupMapper.updataCheckgroup(formData);
    }

    @Override
    public Result delete(Integer id) {
        //删除与检查项 的关联关系
        try {
            checkgroupMapper.deleteAssociation(id);
            checkgroupMapper.deleteEdit(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
}
